/*
 * Copyright 2008-2010 Daniel Cachapa <cachapa@gmail.com>
 * 
 * This program is distributed under the terms of the GNU General Public License Version 3
 * The license can be read in its entirety in the LICENSE.txt file accompanying this source code,
 * or at: http://www.gnu.org/copyleft/gpl.html
 * 
 * This file is part of Libra.
 *
 * WeightWatch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * WeightWatch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the WeightWatch source code. If not, see: http://www.gnu.org/licenses
 */

package net.cachapa.libra;

import net.cachapa.libra.util.MyDate;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;

public class DietPlan extends Activity implements OnClickListener, OnDateSetListener {
	private static final int DIALOG_SELECT_VALUE = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dietplan);
		
		Button initialValueButton = (Button) findViewById(R.id.InitialValueButton);
		initialValueButton.setOnClickListener(this);
		
		Button goalDateButton = (Button) findViewById(R.id.GoalDateButton);
		goalDateButton.setOnClickListener(this);
		
		Button acceptButton = (Button) findViewById(R.id.AcceptButton);
		acceptButton.setOnClickListener(this);
		
		Button cancelButton = (Button) findViewById(R.id.CancelButton);
		cancelButton.setOnClickListener(this);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.InitialValueButton:
			showDialog(DIALOG_SELECT_VALUE);
			break;
		
		case R.id.GoalDateButton:
			MyDate today = new MyDate();
			today.addDays(1);
			DatePickerDialog datePicker = new DatePickerDialog(this, this, today.getYear(), today.getMonth() - 1, today.getDay());
			datePicker.show();
			break;
		
		case R.id.AcceptButton:
			// TODO Create / update the plan
			Toast.makeText(this, "Diet plan created", Toast.LENGTH_SHORT).show();
			finish();
			break;
		
		case R.id.CancelButton:
			finish();
			break;
		}
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_SELECT_VALUE:
			final String[] options = {"Cheese", "Pepperoni", "Black Olives"};
            return new AlertDialog.Builder(this)
                .setTitle("Select initial value")
                .setItems(options, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        /* User clicked so do some stuff */
                        
                        new AlertDialog.Builder(DietPlan.this)
                                .setMessage("You selected: " + which + ": " + options[which])
                                .show();
                    }
                })
                .create();
		}
		
		
		return super.onCreateDialog(id);
	}

	public void onDateSet(DatePicker datePicker, int year, int month, int day) {
		Button goalDateButton = (Button) findViewById(R.id.GoalDateButton);
		MyDate goalDate = new MyDate(day, month + 1, year);
		goalDateButton.setText(goalDate.toString());
	}
}
